<script setup>
import {Delete, Edit} from '@element-plus/icons-vue'
import {ref} from 'vue'
import {
  addArticleCategoryAPI,
  articleCategoryAPI,
  deleteArticleCategoryAPI,
  updateArticleCategoryAPI
} from "../../api/article";
import {ElMessage} from "element-plus";


// 文章分类数组
const categorys = ref([])

// 弹框的显示状态
const dialogVisible = ref(false)

// 弹框的标题
const title = ref('')

// 表单的参数校验
const form = ref(null)

// 单个文章分类数据
const categoryData = ref({
  id: '',
  categoryName: '',
  categoryAlias: ''
})

// 参数校验
const rules = {
  categoryName: [
    {required: true, message: '请输入分类名称', trigger: 'blur'}
  ],
  categoryAlias: [
    {required: true, message: '请输入分类别名', trigger: 'blur'}
  ]
}

// 获取文章分类的函数
const getArticleCategorys = async () => {
  const result = await articleCategoryAPI();
  categorys.value = result.data;
};


// 清空数据
const clearCategoryData = () => {
  categoryData.value = {
    categoryName: '',
    categoryAlias: ''
  }
}

// 添加文章分类
const addCategory = () => {
  // 参数校验通过执行
  form.value.validate(async (valid) => {
    if (valid) {
      // 发送添加文章请求
      const result = await addArticleCategoryAPI(categoryData.value);
      // 提示信息
      ElMessage.success(result.message ? result.message : '文章分类添加成功')
      // 关闭弹框
      dialogVisible.value = false
      // 刷新
      getArticleCategorys()
    }
  })
}

// 编辑弹框数据回显
const edit = (row) => {
  title.value = '修改分类';
  // 打开弹窗
  dialogVisible.value = true
  // 回显数据
  categoryData.value = row
}

// 修改文章
const updateCategory = () => {
  // 参数校验通过执行
  form.value.validate(async (valid) => {
    // 校验通过执行
    if (valid) {
      // 发送修改文章分类请求
      const result = await updateArticleCategoryAPI(categoryData.value);
      // 提示信息
      ElMessage.success(result.message ? result.message : '文章分类修改成功')
      // 关闭弹框
      dialogVisible.value = false
      // 刷新
      getArticleCategorys()
    }
  })
}

// 删除文章
const deleteCategory = async (row) => {
  const result = await deleteArticleCategoryAPI(row.id);
  // 提示信息
  ElMessage.success(result.message ? result.message : '文章分类删除成功')
  // 刷新
  getArticleCategorys()
}


// 执行函数
getArticleCategorys()


</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>文章分类</span>
        <div class="extra">
          <el-button type="primary" @click="title='添加分类';dialogVisible=true;clearCategoryData()">添加分类</el-button>
        </div>
      </div>
    </template>

    <!-- 添加分类弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="30%">
      <el-form ref="form" :model="categoryData" :rules="rules" label-width="100px" style="padding-right: 30px">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="categoryData.categoryName" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="categoryAlias">
          <el-input v-model="categoryData.categoryAlias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
            <el-button @click="getArticleCategorys();dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="title==='添加分类'?addCategory():updateCategory()"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>

    <!--  分类列表  -->
    <el-table :data="categorys" style="width: 100%">
      <el-table-column label="序号" width="100" type="index"></el-table-column>
      <el-table-column label="分类名称" prop="categoryName"></el-table-column>
      <el-table-column label="分类别名" prop="categoryAlias"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="edit(row)"></el-button>
          <el-popconfirm title="你确认删除该文章分类吗？" confirm-button-text="确认" cancel-button-text="取消"
                         @confirm="deleteCategory(row)">
            <template #reference>
              <el-button :icon="Delete" circle plain type="danger"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"/>
      </template>
    </el-table>
  </el-card>
</template>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>